package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.mapper.TbAddressMapper;
import com.dongdongshop.mapper.TbAreasMapper;
import com.dongdongshop.mapper.TbCitiesMapper;
import com.dongdongshop.mapper.TbProvincesMapper;
import com.dongdongshop.pojo.address.TbAddress;
import com.dongdongshop.pojo.order.TbOrder;
import com.dongdongshop.service.AddressServiceI;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Component
@Service
public class AddressServiceImpl implements AddressServiceI {

    @Resource
    private TbAddressMapper addressMapper;

    @Resource
    private TbProvincesMapper provincesMapper;

    @Resource
    private TbCitiesMapper citiesMapper;

    @Resource
    private TbAreasMapper areasMapper;

    @Override
    public TbOrder getReceiver(Long addressId, TbOrder tbOrder) {
        TbAddress address = addressMapper.selectByPrimaryKey(addressId);
        String ProvinceName = provincesMapper.selectByProvincesId(address.getProvinceId());
        String cityName = citiesMapper.selectByCityId(address.getCityId());
        String townName = areasMapper.selectByTownId(address.getTownId());
        String address1 = address.getAddress();
        String receiver = ProvinceName+cityName+townName+address1;
        tbOrder.setReceiverAreaName(receiver);
        tbOrder.setReceiverMobile(address.getMobile());
        tbOrder.setReceiver(address.getContact());
        return tbOrder;
    }

    @Override
    public void isDefault(String username, Long id) {
        addressMapper.setDefault(username);
        addressMapper.isDefault(id,username);
    }

    @Override
    public void insertAddress(TbAddress address) {
        address.setCreateDate(new Date());
        address.setIsDefault("0");
        addressMapper.insertSelective(address);
    }

    @Override
    public void deleteAddress(Long id) {
        addressMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<TbAddress> listAddress(String username) {
        List<TbAddress> addressList=addressMapper.listAddress(username);
        for (TbAddress address : addressList) {
            String provinceName = provincesMapper.selectByProvincesId(address.getProvinceId());
            String cityName = citiesMapper.selectByCityId(address.getCityId());
            String townName = areasMapper.selectByTownId(address.getTownId());
            address.setProvinceName(provinceName);
            address.setCityName(cityName);
            address.setTownName(townName);
        }
        return addressList;
    }
}
